﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DigitalSolutionLibrary.SqlDatabase;
using DigitalSolutionLibrary.Utilities;
using System.Threading;

namespace DigitalSolutionManagement
{
    public partial class frmWelcome:Form
    {
        private Thread th;

        public frmWelcome()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            BaseFormEdit.RefreshEvent += new EventHandler(BaseFormEdit_RefreshEvent);
        }

        void BaseFormEdit_RefreshEvent(object sender, EventArgs e)
        {
            if (sender.ToString() == this.Name)
            {
                processing1.ShowProcessing();
                th = new Thread(() => LoadEmpInfo());
                th.Start();
            }
        }

        private void LoadEmpInfo()
        {
            ParameterStock stock = new ParameterStock();
            Parameter par = new Parameter("empId", Program.EmpId, SqlDbType.Int);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("GetEmployee", stock, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                DataSet ds = (DataSet)obj;
                lblAddress.Text = ds.Tables[0].Rows[0]["EmpAddress"].ToString();
                if (ds.Tables[0].Rows[0]["EmpBirthday"] != DBNull.Value)
                {
                    lblBirthday.Text = ((DateTime)ds.Tables[0].Rows[0]["EmpBirthday"]).ToShortDateString();
                }

                if ((bool)ds.Tables[0].Rows[0]["EmpGender"])
                    lblGender.Text = "Nam";
                else
                    lblGender.Text = "Nữ";

                lblEmpCode.Text = ds.Tables[0].Rows[0]["EmpCode"].ToString();
                lblEmail.Text = ds.Tables[0].Rows[0]["EmpEmail"].ToString();
                lblEmpName.Text = ds.Tables[0].Rows[0]["EmpName"].ToString();
                lblPhone.Text = ds.Tables[0].Rows[0]["EmpPhone"].ToString();
                
                if (ds.Tables[0].Rows[0]["EmpAvatar"] != DBNull.Value)
                {
                    picAvatar.BackgroundImage = null;
                    picAvatar.Image = Utility.ByteToImage((byte[])ds.Tables[0].Rows[0]["EmpAvatar"]);
                }
                processing1.HideProcessing();
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kêt nối đến server, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void frmWelcome_Load(object sender, EventArgs e)
        {
            processing1.ShowProcessing();
            th = new Thread(() => LoadEmpInfo());
            th.Start();
        }

        private void frmWelcome_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th != null && th.IsAlive)
                th.Abort();
        }
    }
}
